/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.ds;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.json.JSONArray;
import org.json.JSONObject;

public class DSServer
{
	public static final String NS = "http://ids.bioteam.net/BiPod/DBNet/Server";
	public static String URL="http://ids.bioteam.net/dbnet/scgi/dbnetserver.cgi";
	//public static final String XSI_NS="http://www.w3.org/2001/XMLSchema-instance";
	//public static final String XSD_NS="http://www.w3.org/2001/XMLSchema";
	public static List<SrcDataset> loadDatasetsForSubscription() throws Exception
	{
		OMFactory fac = null;
		OMNamespace omNs = null;

		ServiceClient sc = new ServiceClient();
		Options options = new Options();
		options.setProperty(HTTPConstants.CHUNKED, false);
		options.setTo(new EndpointReference(URL));
		sc.setOptions(options);

		fac = OMAbstractFactory.getOMFactory();
		omNs = fac.createOMNamespace(NS, "ws");

		sc.getOptions().setAction("http://ids.bioteam.net/BiPod/DBNet/Server#getServerAllDataJSON");
		OMElement method = fac.createOMElement("getServerAllDataJSON", omNs);

		OMElement resp = sc.sendReceive(method);
		String text = resp.getFirstElement().getText();
		JSONArray arr = new JSONArray(text);
		List<SrcDataset> dss = new ArrayList<SrcDataset>(arr.length());
		for (int i = 0; i < arr.length(); i++)
		{
			JSONObject obj = arr.getJSONObject(i);
			SrcDataset src = jsonToSrcDataset(obj);
			dss.add(src);
		}

		Collections.sort(dss, new Comparator<SrcDataset>()
		{

			public int compare(SrcDataset o1, SrcDataset o2)
			{
				return o1.name.compareTo(o2.name);
			}
		});
		return dss;
	}
	
	public static SrcDataset jsonToSrcDataset(JSONObject obj)
	{
		SrcDataset src=new SrcDataset();
		src.id=obj.optLong("idx");
		src.baseurl=obj.optString("baseurl", null);
		src.compression=obj.optString("compression");
		src.displayName=obj.optString("displayname", null);
		src.enabled=obj.optInt("status", 1)==1;
		src.filename=obj.optString("filename", null);
		src.format=obj.optString("format", null);
		src.indexable=obj.optInt("indexable", 0)==1;
		src.name=obj.optString("name", null);
		src.type=obj.optString("type", "N");
		src.staticUrl=obj.optInt("staticurl", 0)==1;
		
		JSONArray arr=obj.optJSONArray("versions");
		if (arr!=null)
		{
			for (int i=0; i<arr.length(); i++)
			{
				JSONObject obj1=arr.optJSONObject(i);
				src.getVersions().add(jsonToVersion(obj1));
			}
		}
		arr=obj.optJSONArray("pacthes");
		if (arr!=null)
		{
			for (int i=0; i<arr.length(); i++)
			{
				JSONObject obj1=arr.optJSONObject(i);
				src.getPatches().add(jsonToPatch(obj1));
			}
		}	
		return src;
		
	}
	
	public static Version jsonToVersion(JSONObject obj)
	{
		Version ver=new Version();
		
		ver.fsize=obj.optLong("fsize");
		ver.letters=obj.optLong("letters");
		ver.md5=obj.optString("md5");
		ver.uncompmd5=obj.optString("uncompmd5");
		ver.seqs=obj.optLong("seqs");
		ver.version=obj.optString("version");
		return ver;
	}
	public static Patch jsonToPatch(JSONObject obj)
	{
		Patch p=new Patch();
		p.curversion=obj.optString("curversion");
		p.difffile=obj.optString("difffile");
		p.diffsize=obj.optLong("diffsize");
		p.patchmd5=obj.optString("patchmd5");
		p.preversion=obj.optString("preversion");
		return p;
		
	}
	
	public static List<SrcDataset> loadDatasetForDownload(List<Integer> ids) throws Exception
	{
		OMFactory fac = null;
		OMNamespace omNs = null;

		ServiceClient sc = new ServiceClient();
		Options options = new Options();
		options.setProperty(HTTPConstants.CHUNKED, false);
		options.setTo(new EndpointReference(URL));
		sc.setOptions(options);

		fac = OMAbstractFactory.getOMFactory();
		omNs = fac.createOMNamespace(NS, "ws");
		//OMNamespace xsiNs = fac.createOMNamespace(XSI_NS, "xsi");
		//OMNamespace xsdNs = fac.createOMNamespace(XSD_NS, "xsd");
		//OMAttribute attr=fac.createOMAttribute("type", xsiNs, "xsd:int");
		
		sc.getOptions().setAction("http://ids.bioteam.net/BiPod/DBNet/Server#getSubscribedServerDataJSON");
		OMElement method = fac.createOMElement("getSubscribedServerDataJSON", omNs);
		OMElement value;
		
		for (Integer id:ids)
		{
			value= fac.createOMElement("id", omNs);	
			value.addChild(fac.createOMText(value, String.valueOf(id)));
			//value.addAttribute(attr);
			method.addChild(value);
		}
		

		OMElement resp = sc.sendReceive(method);
		String text = resp.getFirstElement().getText();
		JSONArray arr = new JSONArray(text);
		List<SrcDataset> dss = new ArrayList<SrcDataset>(arr.length());
		for (int i = 0; i < arr.length(); i++)
		{
			JSONObject obj = arr.getJSONObject(i);
			SrcDataset src = jsonToSrcDataset(obj);
			dss.add(src);
		}
		return dss;
	}

}
